package com.example.sunsg.okhttptest.adapter;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;

import com.example.sunsg.okhttptest.util.Util;

/**
 * Created by NOTE-026 on 2016/5/10.
 */
public class RecyclerDividerItemDecoradion extends RecyclerView.ItemDecoration {

    private Context mContext;
    /**
     * 高度
     */
    private int mDiviverHeight;


    public RecyclerDividerItemDecoradion(Context context, int diviverHeight) {
        mContext = context;
        this.mDiviverHeight = Math.max(Util.dp2px(context, diviverHeight), 0);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        if(isLastSpan(view,parent)){
            //最后一列
            outRect.set(0, 0, 0, mDiviverHeight);
        }else if(isLastRaw(view,parent)){
            //最后一行
            outRect.set(0, 0, mDiviverHeight, 0);
        }else{
            outRect.set(0, 0, mDiviverHeight, mDiviverHeight);
        }
        Log.i("test","position = "+parent.getChildAdapterPosition(view));
//        if (manager instanceof GridLayoutManager) {
//            int span = ((GridLayoutManager) manager).getSpanCount();
//            int raw = (span == 0 ? 0 : parent.getAdapter().getItemCount()/span);
//            int position = parent.getChildAdapterPosition(view);
//            Log.i("test","position = "+position);
//            if((position % span + 1) == span){
//                //最后一列
//                outRect.set(0, 0, 0, mDiviverHeight);
//            }else if(raw == position/span){
//                //最后一行
//                outRect.set(0, 0, mDiviverHeight, 0);
//            }else{
//                outRect.set(0, 0, mDiviverHeight, mDiviverHeight);
//            }
////
//        }else if(manager instanceof StaggeredGridLayoutManager){
//
//        }
    }

    private boolean isLastSpan(View childView,RecyclerView parent){
        boolean isLastSpan = false;
        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        if(manager instanceof GridLayoutManager){
            int span = ((GridLayoutManager) manager).getSpanCount();//几列

            int count = parent.getAdapter().getItemCount();//item的个数

            int position = parent.getChildAdapterPosition(childView);//view在adapter中位置

            int oriation = ((GridLayoutManager) manager).getOrientation();//方向

            if(GridLayoutManager.VERTICAL == oriation){
                //满足一下是组后一列
                if(((position +1 ) % span) == 0){
                    isLastSpan = true;
                }
            }else if(GridLayoutManager.HORIZONTAL == oriation){
                int colum = count/span;
                if(position/ span == colum){
                    isLastSpan = true;
                }
            }

        }else if(manager instanceof StaggeredGridLayoutManager){
            int span = ((StaggeredGridLayoutManager) manager).getSpanCount();//纵向为几列 横向为几行

            int count = parent.getAdapter().getItemCount();//item的个数

            int position = parent.getChildAdapterPosition(childView);//view在adapter中位置

            int oriation = ((StaggeredGridLayoutManager) manager).getOrientation();//方向

            if(oriation == StaggeredGridLayoutManager.VERTICAL){
                if(((position +1 ) % span) == 0){
                    isLastSpan = true;
                }
            }else if(oriation == StaggeredGridLayoutManager.HORIZONTAL){
                int colum = count/span;
                if(position/ span == colum){
                    isLastSpan = true;
                }

            }
        }
        return isLastSpan;
    }

    private boolean isLastRaw(View childView,RecyclerView parent){
        boolean isLastRaw = false;
        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        if(manager instanceof GridLayoutManager){
            int span = ((GridLayoutManager) manager).getSpanCount();//几列

            int count = parent.getAdapter().getItemCount();//item的个数

            int position = parent.getChildAdapterPosition(childView);//view在adapter中位置

            int oriation = ((GridLayoutManager) manager).getOrientation();//方向

            if(GridLayoutManager.VERTICAL == oriation){//纵向
                //满足以下是组后一行
                int colum = count/span;
                if(position/ span == colum){
                    isLastRaw = true;
                }

            }else if(GridLayoutManager.HORIZONTAL == oriation){//横向
                //满足以下是组后一行
                if(((position +1 ) % span) == 0){
                    isLastRaw = true;
                }
            }

        }else if(manager instanceof StaggeredGridLayoutManager){
            int span = ((StaggeredGridLayoutManager) manager).getSpanCount();//纵向为几列 横向为几行

            int count = parent.getAdapter().getItemCount();//item的个数

            int position = parent.getChildAdapterPosition(childView);//view在adapter中位置

            int oriation = ((StaggeredGridLayoutManager) manager).getOrientation();//方向

            if(oriation == StaggeredGridLayoutManager.VERTICAL){//纵向
                int colum = count/span;
                if(position/ span == colum){
                    isLastRaw = true;
                }

            }else if(oriation == StaggeredGridLayoutManager.HORIZONTAL){//横向
                if(((position +1 ) % span) == 0){
                    isLastRaw = true;
                }
            }
        }
        return isLastRaw;
    }


}